﻿using UnityEngine;
using System.Collections;

public class Mario : MonoBehaviour {

	Mob mob;
	public int bar;
	int barnext;
	public int runpos;

	enum MarioState {
		Idle,
		Barhopping,
		Tapping,
		Throwing,
		Running,
		Retreating,
	};
	MarioState state = MarioState.Idle;

	bool tapheld;
	bool tapreleased;
	bool tapqueued;
	int taptimer;
	int throwtimer;
	int hoptimer;

	//public bool keyu = false;
	//public bool keyd = false;

	
	public void create() {
		mob = new Mob ();
		mob.create ("Mario");
	}

	public void init() {
		bar = 0;
		state = MarioState.Barhopping;
		hoptimer = 0;
		//bar = Tappa.game.barslen-1;
		barnext = 0;
		mob.init (Tappa.game.data.player);
		upd ();
	}

	public void upd() {
		//(Input.GetAxis ("vertical") < 0);
		bool keyu = Input.GetKey (KeyCode.E) || Input.GetKey (KeyCode.UpArrow);
		bool keyd = Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.DownArrow);
		bool keyt = Input.GetKey (KeyCode.F) || Input.GetKey (KeyCode.RightArrow);
		bool keys = Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.LeftArrow);

		if (state == MarioState.Idle) {
			mob.dataSprite = Tappa.game.data.player;
			Data_Bar b = Tappa.game.bars[bar].data;
			mob.pos = b.playerHome.pos;
			xy pos1 = b.playerHome.pos;
			xy pos2 = b.playerEnd.pos;
			pos2.x = b.playerHome.pos.x + (b.beerEnd.pos.x - b.beerStart.pos.x);
			pos2.y = b.playerHome.pos.y + (b.beerEnd.pos.y - b.beerStart.pos.y);
			if (keys) {
				runpos += Tappa.game.data.constants.playerSpeedWalk;
			} else if (keyt) {
				runpos -= Tappa.game.data.constants.playerSpeedWalk;
			}
			runpos = (int)Mathf.Clamp (runpos, 0, Tappa.game.bars[bar].data.barLengthScale);
			float tween = Mathf.Clamp01 ((float)runpos / Tappa.game.bars[bar].data.barLengthScale);
			mob.pos.x = (int)Mathf.Lerp (pos1.x, pos2.x, tween);
			mob.pos.y = (int)Mathf.Lerp (pos1.y, pos2.y, tween);
			if (keyd) {
				state = MarioState.Barhopping;
				barnext = (bar + 1) % Tappa.game.barslen;
				hoptimer = 0;
				runpos = 0;
			} else if (keyu) {
				state = MarioState.Barhopping;
				barnext = (bar - 1 + Tappa.game.barslen) % Tappa.game.barslen;
				hoptimer = 0;
				runpos = 0;
			} else if (keyt && runpos == 0) {
				state = MarioState.Tapping;
				taptimer = 0;
				Tappa.game.bars[bar].isTapping = true;
			}
		}

		if (state == MarioState.Running) {
		}

		if (state == MarioState.Barhopping) {
			mob.dataSprite = Tappa.game.data.playerMoving;
			xy pos;
			Data_Bar b1 = Tappa.game.bars[bar].data;
			Data_Bar b2 = Tappa.game.bars[barnext].data;
			xy pos1 = b1.playerHome.pos;
			xy pos2 = b2.playerHome.pos;
			float tween = Mathf.Clamp01 (2.0f*hoptimer / Tappa.game.data.constants.playerAnimTimeMove);
			pos.x = (int)Mathf.Lerp (pos1.x, pos2.x, tween);
			pos.y = (int)Mathf.Lerp (pos1.y, pos2.y, tween);
			mob.pos = pos;
			hoptimer++;
			if (hoptimer > Tappa.game.data.constants.playerAnimTimeMove) {
				bar = barnext;
				mob.pos = pos2;
				state = MarioState.Idle;
			}
		}

		if (state == MarioState.Tapping) {
			mob.dataSprite = Tappa.game.data.playerTapping;
			taptimer++;
			if (taptimer > Tappa.game.data.constants.playerAnimTimeTap) {
				taptimer = Tappa.game.data.constants.playerAnimTimeTap;
				if (!keyt) {

					state = MarioState.Throwing;
					throwtimer = 0;

					Tappa.game.bars[bar].isTapping = false;

					Beer t = Tappa.game.beers.get ();
					t.init (Tappa.game.bars[bar]);

				}
			}
		}

		if (state == MarioState.Throwing) {
			mob.dataSprite = Tappa.game.data.playerThrowing;
			throwtimer++;
			if (throwtimer > Tappa.game.data.constants.playerAnimTimeThrow) {
				state = MarioState.Idle;
			}
		}

		if (state == MarioState.Running) {
		}
		if (state == MarioState.Retreating) {
		}

		mob.upd ();

	}

}


